﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using DPMP.DataAccess;
using System.Data;
using System.Windows.Input;

namespace DPMP.ViewModel
{
    public class PatternModelViewModel : TreeViewItemViewModel
    {
        #region Private Fields

        // business model of this pattern model
        PatternModel _patternModel;

        // add new instance command
        ICommand _addNewInstanceCommand;

        #endregion

        #region Constructors

        public PatternModelViewModel(PatternModel patternModel)
            : base(null, true)
        {
            // set pattern model
            _patternModel = patternModel;

            // set add new instance command
            _addNewInstanceCommand = new PatternModelAddNewInstanceCommand(this);
        }

        #endregion

        #region Properties

        // name of the pattern model
        public string PatternModelName
        {
            get { return _patternModel.Name; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// get the table of properties of this pattern model
        /// </summary>
        /// <returns></returns>
        public override DataTable GetProperties()
        {
            // create new data table
            DataTable dtProperties = new DataTable();

            // create property name column
            DataColumn colPropertyName = new DataColumn("Property", typeof(string));
            dtProperties.Columns.Add(colPropertyName);

            // create property value column
            DataColumn colPropertyValue = new DataColumn("Value", typeof(string));
            dtProperties.Columns.Add(colPropertyValue);

            // add model name row
            DataRow rowInstanceName = dtProperties.NewRow();
            dtProperties.Rows.Add(rowInstanceName);
            rowInstanceName["Property"] = "Model Name";
            rowInstanceName["Value"] = _patternModel.Name;

            // add model description row
            DataRow rowDescription = dtProperties.NewRow();
            dtProperties.Rows.Add(rowDescription);
            rowDescription["Property"] = "Description";
            rowDescription["Value"] = _patternModel.Description;

            // add XFrame File Path row
            DataRow rowXFrameFilePath = dtProperties.NewRow();
            dtProperties.Rows.Add(rowXFrameFilePath);
            rowXFrameFilePath["Property"] = "XFrame File Path";
            rowXFrameFilePath["Value"] = Service.GetFullPath(_patternModel.XFrameFilePath);

            // add SPC Template File Path row
            DataRow SPCTemplateFilePath = dtProperties.NewRow();
            dtProperties.Rows.Add(SPCTemplateFilePath);
            SPCTemplateFilePath["Property"] = "SPC Template File Path";
            SPCTemplateFilePath["Value"] = Service.GetFullPath(_patternModel.SPCTemplateFilePath);

            // add Designer Template File Path row
            DataRow rowDesignerTemplateFilePath = dtProperties.NewRow();
            dtProperties.Rows.Add(rowDesignerTemplateFilePath);
            rowDesignerTemplateFilePath["Property"] = "Designer Template File Path";
            rowDesignerTemplateFilePath["Value"] = Service.GetFullPath(_patternModel.DesignerTemplateFilePath);

            // return the data table of properties
            return dtProperties;
        }

        #endregion

        #region Command

        #region Add New Instance Command

        /// <summary>
        /// return the command used to show the property of a pattern instance
        /// </summary>
        public ICommand AddNewInstanceCommand
        {
            get { return _addNewInstanceCommand; }
        }

        /// <summary>
        /// handle the showing properties of pattern instance
        /// </summary>
        private class PatternModelAddNewInstanceCommand : ICommand
        {
            PatternModelViewModel _patternModel;

            public PatternModelAddNewInstanceCommand(PatternModelViewModel patternModel)
            {
                _patternModel = patternModel;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            event EventHandler ICommand.CanExecuteChanged
            {
                add { }
                remove { }
            }

            public void Execute(object parameter)
            {
                //System.Windows.MessageBox.Show("Testing Add New Instance");
            }
        }

        #endregion

        #endregion

        #region Protected Methods

        protected override void LoadChildren()
        {
            foreach (PatternInstance patternInstance in _patternModel.PatternInstances)
            {
                base.Children.Add(new PatternInstanceViewModel(patternInstance, this));
            }
        }

        #endregion
    }
}
